/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : WifiSoc.c
 * Author        : joki.zhu
 * Date          : 2020-07-03
 *
 * Record        :
 * 1.Date        : 2020-07-03
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/
#include "WifiSoc.h"
#include "app_common.h"
#include "Upgrade.h"

#ifdef YD_PROTOCOL
#include "DOTSdkMedia.h"
#endif /* YD_PROTOCOL */

#define WIFI_SOC_FAILED_MAX_NUM     (2)

// static int gs_batteryLevel[10] = {1577, 1602, 1615, 1624, 1637, 1658, 1680, 1705, 1740, 1800};
 static int gs_batteryLevel[10] = {1436, 1472, 1492, 1507, 1525, 1555, 1595, 1641, 1690, 1790};

static char g_bWifiResetEnable = 1;

 int wifisoc_set_wifi_connect(char *ssid, char *pwd, WIFI_ENCRYPT_TYPE_E enEncryptType)
 { 
    AV_RETURN_IF_PTR_NULL(ssid, AV_FAILURE);
    AV_RETURN_IF_PTR_NULL(pwd, AV_FAILURE);

    LOGI("ssid: %s pwd: %s\n", ssid, pwd);

    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }
    
    int ret = 0;
    HI_SYSLINK_PARM_S stSendMsg;
    HI_SYSLINK_WIFI_ATTR stWifiAttr = {0};

    memset(&stSendMsg, 0, sizeof(stSendMsg));
    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_WIFI_CONNECT;

    snprintf(stWifiAttr.ssid, sizeof(stWifiAttr.ssid), "%s", ssid);
    snprintf(stWifiAttr.pwd, sizeof(stWifiAttr.pwd), "%s", pwd);

    if (0 == strlen(pwd)) {
        enEncryptType = WIFI_ENCRYPT_OPEN;
    } else {
        enEncryptType = WIFI_ENCRYPT_WPA_WPA2;
    }
    
    switch (enEncryptType)
    {
        case WIFI_ENCRYPT_OPEN:
            stWifiAttr.auth = HI_SYSLINK_WIFI_SECURITY_OPEN;
            break;
        case WIFI_ENCRYPT_WPA:
            stWifiAttr.auth = HI_SYSLINK_WIFI_SECURITY_WPA;
            break;
        case WIFI_ENCRYPT_WPA2:
            stWifiAttr.auth = HI_SYSLINK_WIFI_SECURITY_WPA2;
            break;
        case WIFI_ENCRYPT_WEP:
            stWifiAttr.auth = HI_SYSLINK_WIFI_SECURITY_WEP;
            break;
        case WIFI_ENCRYPT_WPA_WPA2:
            stWifiAttr.auth = HI_SYSLINK_WIFI_SECURITY_WPAPSK_WPA2PSK_MIX;
            break;
    }

    memcpy(stSendMsg.data, &stWifiAttr, sizeof(stWifiAttr));
    stSendMsg.dataLen = sizeof(stWifiAttr);

    ret = HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);
    if (ret != 0) {
        LOGE("HI_HAL_SYSLINK_SendMsgToWifiAsync failed\n");
        return -1;
    }

    return 0;
 }

 int wifisoc_set_wifi_disconnect(void)
 {
    HI_SYSLINK_PARM_S stSendMsg;

    LOGI("wifi disconnect...\n");
    
    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }

    memset(&stSendMsg, 0, sizeof(stSendMsg));
    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_WIFI_DISCONNECT;
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);

    return 0;
 }

 int wifisoc_get_wifi_status(HI_SYSLINK_WIFI_STATUS *pstWifiStatus)
{
    static char errCnt = 0;
    HI_SYSLINK_PARM_S stSendMsg = {0};
    HI_SYSLINK_PARM_S stWaitMsg = {0};

    AV_RETURN_IF_PTR_NULL(pstWifiStatus, AV_FAILURE);

    if (SocSubCheckIsUpgrading()) {
//        LOGW("wifi module is upgrading!\n");
//        pstWifiStatus->state = 1;
        return 0;
    }
    
    memset(pstWifiStatus, 0, sizeof(HI_SYSLINK_WIFI_STATUS));
    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_GET_WIFI_STATUS;
    stWaitMsg.dataLen = sizeof(HI_SYSLINK_WIFI_STATUS);
    HI_S32 ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, 1000);
    if (ret != HI_SUCCESS) {
        MLOGE("HI_HAL_SYSLINK_SendMsgToWifiSync fail ret %d\n", ret);
        usleep(100 * 1000);
        if (++errCnt >= WIFI_SOC_FAILED_MAX_NUM) {
            wifisoc_set_reset("wifisoc_get_wifi_status failed");
        }
        return ret;
    }
    errCnt = 0;
    
    memcpy(pstWifiStatus, stWaitMsg.data, stWaitMsg.dataLen);

    LOGD("ssid:%s rssi:%d state:%d\n", pstWifiStatus->ssid, pstWifiStatus->rssi, pstWifiStatus->state);

    return AV_SUCCESS;
}


 int wifisoc_set_led_state(unsigned char value)
{
     HI_SYSLINK_PARM_S stSendMsg = {0};

    if (SocSubCheckIsUpgrading()) {
//        LOGW("wifi module is upgrading!\n");
        return 0;
    }
    
    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_LED;
    stSendMsg.data[0] = value;
    stSendMsg.dataLen = 1;
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);

    LOGD("set led state: %#x\n", value);

    return 0;
 }


 int wifisoc_get_version(char *pVersion)
{
    AV_RETURN_IF_PTR_NULL(pVersion, AV_FAILURE);

    static char errCnt = 0;
    HI_SYSLINK_PARM_S stSendMsg = {0};
    HI_SYSLINK_PARM_S stWaitMsg = {0};

    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }
    
    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_GET_VER;
    stWaitMsg.dataLen = 16;
    HI_S32 ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, 1000);
    if (ret != HI_SUCCESS) {
        if (++errCnt >= WIFI_SOC_FAILED_MAX_NUM) {
            wifisoc_set_reset("wifisoc_get_version failed");
        }
    }
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_SendMsgToWifiSync");
    errCnt = 0;

    memcpy(pVersion, stWaitMsg.data, stWaitMsg.dataLen);

    LOGD("wifi version: %s\n", pVersion);

    return AV_SUCCESS;
 }

 int wifisoc_get_battery_status(BATTERY_STATUS_S *pstBatStatus)
 {
    AV_RETURN_IF_PTR_NULL(pstBatStatus, AV_FAILURE);

    static char errCnt = 0;
    HI_SYSLINK_PARM_S stSendMsg = {0};
    HI_SYSLINK_PARM_S stWaitMsg = {0};
    unsigned short batValue = 0;
    HI_SYSLINK_BATTERY_STATUS stHiBatStatus = { 0 };
    static BATTERY_STATUS_S lastBatStatus = {0, BAT_DISCHARGING, 0};

    memset(pstBatStatus, 0, sizeof(BATTERY_STATUS_S));
    if (SocSubCheckIsUpgrading()) {
//        LOGW("wifi module is upgrading!\n");
        pstBatStatus->capacity = lastBatStatus.capacity;
        pstBatStatus->chargingStatus = lastBatStatus.chargingStatus;
        pstBatStatus->isLowBatState = lastBatStatus.isLowBatState;
        return 0;
    }

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_GET_BATTERY_STATUS;
    stWaitMsg.dataLen = sizeof(stHiBatStatus);
    HI_S32 ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, 1000);
    if (ret != HI_SUCCESS) {
        usleep(100 * 1000);
        if (++errCnt >= WIFI_SOC_FAILED_MAX_NUM) {
            wifisoc_set_reset("wifisoc_get_battery_status failed");
        }
    }
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_SendMsgToWifiSync");
    errCnt = 0;
    
    memcpy(&stHiBatStatus, stWaitMsg.data, sizeof(stHiBatStatus));

    batValue = stHiBatStatus.capacity;
#ifdef BATTERY_TEST
     pstBatStatus->capacity = batValue;
     return 0;
#endif /* BATTERY_TEST */

     if (batValue >= gs_batteryLevel[9]) {
         pstBatStatus->capacity = 100;
     } else if (batValue >= gs_batteryLevel[8]) {
         pstBatStatus->capacity = 90;
     } else if (batValue >= gs_batteryLevel[7]) {
         pstBatStatus->capacity = 80;
     } else if (batValue >= gs_batteryLevel[6]) {
         pstBatStatus->capacity = 70;
     } else if (batValue >= gs_batteryLevel[5]) {
         pstBatStatus->capacity = 60;
     } else if (batValue >= gs_batteryLevel[4]) {
         pstBatStatus->capacity = 50;
     } else if (batValue >= gs_batteryLevel[3]) {
         pstBatStatus->capacity = 40;
     } else if (batValue >= gs_batteryLevel[2]) {
         pstBatStatus->capacity = 30;
     } else if (batValue >= gs_batteryLevel[1]) {
         pstBatStatus->capacity = 20;
         pstBatStatus->isLowBatState = 1;
     } else if (batValue >= gs_batteryLevel[0]) {
         pstBatStatus->capacity = 10;
         pstBatStatus->isLowBatState = 1;
     } else {
         pstBatStatus->capacity = 0;
         pstBatStatus->isLowBatState = 1;
     }
     
#if 1 
     if (batValue < 1000) { //用来判断是否接的是USB供电
         pstBatStatus->capacity = 100;
         pstBatStatus->isLowBatState = 0;
     }
#endif

     if (lastBatStatus.capacity != 0 && pstBatStatus->capacity > lastBatStatus.capacity) {
         pstBatStatus->capacity = lastBatStatus.capacity;
//         pstBatStatus->chargingStatus = lastBatStatus.chargingStatus;
         pstBatStatus->isLowBatState = lastBatStatus.isLowBatState;
     }
     pstBatStatus->chargingStatus = (stHiBatStatus.bUsbCharging == 1) ? BAT_CHARGING : BAT_DISCHARGING;

     lastBatStatus.capacity = pstBatStatus->capacity;
     lastBatStatus.chargingStatus = pstBatStatus->chargingStatus;
     lastBatStatus.isLowBatState = pstBatStatus->isLowBatState;
     
     LOGI("voltage:%dmV capacity:%d%% isLowBatState:%d chargingStatus:%d\n", 
                (int)batValue, pstBatStatus->capacity, pstBatStatus->isLowBatState, pstBatStatus->chargingStatus);

     return 0;
 }


 int wifisoc_set_system_mode(HI_SYSLINK_SYS_MODE enSysMode)
 {
    int ret = 0;
    HI_SYSLINK_PARM_S stSendMsg = {0};

    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }

    LOGD("set system mode: %d\n", (HI_U8)enSysMode);
    
    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_SYSTEM;
    stSendMsg.data[0] = (HI_U8)enSysMode;
    stSendMsg.dataLen = 1;
    ret = HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);
    if (ret != 0) {
        LOGE("HI_HAL_SYSLINK_SendMsgToWifiAsync failed\n");
        return -1;
    }
    
    return 0;
 }

 int wifisoc_set_pir(char bEnable)
 {
    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }

#if 0
    char bWifiPirEnalbe = 0;
    wifisoc_get_pir(&bWifiPirEnalbe);

    if (bEnable == bWifiPirEnalbe) {
        LOGI("wifi pir bEnable (%d) == bWifiPirEnalbe (%d)\n", bEnable, bWifiPirEnalbe);
        return 0;
    }
#endif

    HI_SYSLINK_PARM_S stSendMsg = {0};

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_PIR;
    stSendMsg.data[0] = (HI_U8)bEnable;
    stSendMsg.dataLen = 1;
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);

    LOGD("set pir state: %d\n", (HI_U8)bEnable);

    return 0;
 }

 int wifisoc_get_pir(char *bEnable)
 {
     AV_RETURN_IF_PTR_NULL(bEnable, AV_FAILURE);

    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }
    
    static char errCnt = 0;
    HI_SYSLINK_PARM_S stSendMsg = {0};
    HI_SYSLINK_PARM_S stWaitMsg = {0};

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_GET_PIR;
    stWaitMsg.dataLen = 1;
    HI_S32 ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, 1000);
    if (ret != HI_SUCCESS) {
        if (++errCnt >= WIFI_SOC_FAILED_MAX_NUM) {
            wifisoc_set_reset("wifisoc_get_pir failed");
        }
    }
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_SendMsgToWifiSync");
    errCnt = 0;
    
    *bEnable = stWaitMsg.data[0];

    LOGD("bPirEnable: %d\n", *bEnable);

    return AV_SUCCESS;
 }


int wifisoc_get_time(SYSTEM_TIME *pstSysTime)
{
    AV_RETURN_IF_PTR_NULL(pstSysTime, AV_FAILURE);

    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }
    
    int ret = 0;
    static char errCnt = 0;
    HI_U32 timeoutMs = 1000;
    HI_SYSLINK_PARM_S  stSendMsg = {0};
    HI_SYSLINK_PARM_S  stWaitMsg = {0};
    HI_SYSLINK_DATE_TIME stDataTime;

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_GET_TIME;
    stWaitMsg.dataLen = sizeof(stDataTime);

    ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, timeoutMs);
    if (ret != HI_SUCCESS) {
        if (++errCnt >= WIFI_SOC_FAILED_MAX_NUM) {
            wifisoc_set_reset("wifisoc_get_time failed");
        }
    }
    errCnt = 0;

    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_SendMsgToWifiSync");

    LOGD("%x %x %x %x %x %x len:%d\n", stWaitMsg.data[0], stWaitMsg.data[1],stWaitMsg.data[2],stWaitMsg.data[3],stWaitMsg.data[4],
                                stWaitMsg.data[5], stWaitMsg.dataLen);

    memset(&stDataTime, 0, sizeof(stDataTime));                                   
    memcpy(&stDataTime, stWaitMsg.data, stWaitMsg.dataLen);
    pstSysTime->year = stDataTime.year + 2000;
    pstSysTime->month = stDataTime.month;
    pstSysTime->day = stDataTime.day;
    pstSysTime->hour = stDataTime.hour;
    pstSysTime->minute = stDataTime.minute;
    pstSysTime->second = stDataTime.second;

    LOGD("get wifi time: %02d-%02d-%02d %02d:%02d:%02d\n", pstSysTime->year, pstSysTime->month, pstSysTime->day,
                                               pstSysTime->hour, pstSysTime->minute, pstSysTime->second);    
    return 0;
}
 
 int wifisoc_set_time(SYSTEM_TIME stSysTime)
{
    HI_SYSLINK_PARM_S sendMsg;
    HI_SYSLINK_DATE_TIME stDataTime;

    if (SocSubCheckIsUpgrading()) {
        LOGW("wifi module is upgrading!\n");
        return 0;
    }

    memset(&stDataTime, 0, sizeof(stDataTime));
    stDataTime.year = stSysTime.year - 2000;
    stDataTime.month = stSysTime.month;
    stDataTime.day = stSysTime.day;
    stDataTime.hour = stSysTime.hour;
    stDataTime.minute = stSysTime.minute;
    stDataTime.second = stSysTime.second;

    memset(&sendMsg, 0, sizeof(sendMsg));
    sendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_TIME;
    memcpy(sendMsg.data, &stDataTime, sizeof(stDataTime));
    sendMsg.dataLen = sizeof(stDataTime);
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&sendMsg);

    LOGD("set wifi time: %02d-%02d-%02d %02d:%02d:%02d\n", stSysTime.year, stSysTime.month, stSysTime.day,
                                                   stSysTime.hour, stSysTime.minute, stSysTime.second);
    return 0;
}


 int wifisoc_get_wakeup_type(SYSTEM_WAKEUP_TYPE_E *pWakeUpType)
 {
    AV_RETURN_IF_PTR_NULL(pWakeUpType, AV_FAILURE);

    int ret = 0;
    HI_SYSLINK_WAKEUP_TYPE enHiWakeupType = HI_SYSLINK_WAKEUP_BUFF;

    static char bGetWakeupType = 0;
    static SYSTEM_WAKEUP_TYPE_E sWakeUpType = SYS_WAKEUP_TYPE_BUFF;

    if (bGetWakeupType) {
        *pWakeUpType = sWakeUpType;
    }

    if (SocSubCheckIsUpgrading()) {
//        LOGW("wifi module is upgrading!\n");
        return 0;
    }

    ret = HI_HAL_SYSLINK_GetWakeupType(&enHiWakeupType);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_GetWakeupType");

    bGetWakeupType = 1;
    
    switch (enHiWakeupType)
    {
        case HI_SYSLINK_WAKEUP_PIR:
            *pWakeUpType = SYS_WAKEUP_TYPE_PIR;
            break;
        case HI_SYSLINK_WAKEUP_KEY_BELL:
            *pWakeUpType = SYS_WAKEUP_TYPE_KEY_BELL;
            break;
        case HI_SYSLINK_WAKEUP_KEY_NETCFG:
            *pWakeUpType = SYS_WAKEUP_TYPE_KEY_NETCFG;
            break;
        case HI_SYSLINK_WAKEUP_KEY_PICK:
            *pWakeUpType = SYS_WAKEUP_TYPE_KEY_PICK;
            break;
        case HI_SYSLINK_WAKEUP_WIFI:
            *pWakeUpType = SYS_WAKEUP_TYPE_WIFI;
            break;
        case HI_SYSLINK_WAKEUP_RTC:
            *pWakeUpType = SYS_WAEKUP_TYPE_ALARM;
            break;  
        case HI_SYSLINK_WAKEUP_POWERON:
            *pWakeUpType = SYS_WAKEUP_TYPE_POWER_ON;
            break;
        case HI_SYSLINK_WAKEUP_LOW_POWER:
            *pWakeUpType = SYS_WAKEUP_TYPE_LOW_POWER;
            break;
        case HI_SYSLINK_WAKEUP_USB_CHARGE:
            *pWakeUpType = SYS_WAKEUP_TYPE_USB_CHARGE;
            break;
        case HI_SYSLINK_WAKEUP_BUFF:
        default:
            LOGW("WakeUpType %d is invalid\n", enHiWakeupType);
            break;
    }

    sWakeUpType = *pWakeUpType;
    
    /* clear wakeup info */
    wifisoc_clear_wakeup_info();
    
    return 0;
 }

int  wifisoc_set_keepalive(HI_SYSLINK_WIFI_KEEPALIEVE_S stKeepAlive)
{
     HI_SYSLINK_PARM_S sendMsg;
     
     if (SocSubCheckIsUpgrading()) {
         LOGW("wifi module is upgrading!\n");
         return 0;
     }
     
     memset(&sendMsg, 0, sizeof(sendMsg));
     sendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_WIFI_KEEPALIVE;
     memcpy(sendMsg.data, &stKeepAlive, sizeof(stKeepAlive));
     sendMsg.dataLen = sizeof(stKeepAlive);
     HI_HAL_SYSLINK_SendMsgToWifiAsync(&sendMsg);

    return 0;
}

int wifisoc_clear_wakeup_info(void)
{
    HI_SYSLINK_PARM_S  stSendMsg = {0};

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_CLEAR_WAKEUP_INFO;
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);
    
    return AV_SUCCESS;
}


int wifisoc_set_wifi_tty_enable(void)
{
    HI_SYSLINK_PARM_S  stSendMsg = {0};

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_TTY_ENABLE;
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);

    usleep(100 * 1000);
    hi_syslink_enable_tty(HI_SYSLINK_TTY_AT, HI_TRUE);
    hi_syslink_attach_tty(HI_SYSLINK_TTY_AT);

    return AV_SUCCESS;
}

int wifisoc_set_reset(char *reason)
{
    if (!SocSubCheckIsUpgrading() && g_bWifiResetEnable == 1) {
        LOGW("reset wifi the reason: %s\n", reason);
        LOGW("Reset wifi !!!\n");
        usleep(100 * 1000);
        GpioSet(PRODUCT_GPIO_RESET_MCU);
    }

    return 0;
}

int wifisoc_set_reset_ex(char *reason)
{
    if (!UpgradeCheckIsRun() && g_bWifiResetEnable == 1) {
        LOGW("reset wifi the reason: %s\n", reason);
        himm(0x112C0050, 0x0);  //PRODUCT_GPIO_RESET_MCU GPIO8_6
        himm(0x120B8400, 0x40);
        usleep(100 * 1000);
        himm(0x120B8100, 0x0);
    }

    return 0;
}

void wifisoc_set_reset_switch(char bEnable)
{
    g_bWifiResetEnable = bEnable;
}

int wifisoc_upgrade_start(HI_SYSLINK_UPGRADE_START_MSG stUpgStartMsg)
{
    HI_SYSLINK_PARM_S  stSendMsg = {0};
    HI_SYSLINK_PARM_S  stWaitMsg = {0};
    HI_U32 timeoutMs = 5000;

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_UPGRADE_START;
    stSendMsg.dataLen = sizeof(stUpgStartMsg);
    stWaitMsg.dataLen = 1;
    memcpy(stSendMsg.data, &stUpgStartMsg, stSendMsg.dataLen);
    HI_S32 ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, timeoutMs);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_SendMsgToWifiSync");
    if (stWaitMsg.data[0] != HI_SYSLINK_UPGRADE_ACK_OK) {
        LOGE("stWaitMsg.data[0]: 0x%x\n", stWaitMsg.data[0]);
        return AV_FAILURE;
    }

    return AV_SUCCESS;
}

int wifisoc_upgrade_transmit(HI_SYSLINK_UPGRADE_TRANSMIT_MSG stUpgTransMsg)
{
    HI_SYSLINK_PARM_S  stSendMsg = {0};
    HI_SYSLINK_PARM_S  stWaitMsg = {0};
    HI_U32 timeoutMs = 5000;

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_UPGRADE_TRANSMIT;
    stSendMsg.dataLen = sizeof(stUpgTransMsg);
    stWaitMsg.dataLen = 1;
    memcpy(stSendMsg.data, &stUpgTransMsg, stSendMsg.dataLen);
    HI_S32 ret = HI_HAL_SYSLINK_SendMsgToWifiSync(&stSendMsg, &stWaitMsg, timeoutMs);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == HI_SUCCESS, AV_FAILURE, "HI_HAL_SYSLINK_SendMsgToWifiSync");
    if (stWaitMsg.data[0] != HI_SYSLINK_UPGRADE_ACK_OK) {
        LOGE("stWaitMsg.data[0]: 0x%x\n", stWaitMsg.data[0]);
        return AV_FAILURE;
    }

    return AV_SUCCESS;
}

int wifisoc_upgrade_finish(void)
{
    HI_SYSLINK_PARM_S  stSendMsg = {0};

    stSendMsg.u32MsgId = HI_SYSLINK_MSGID_S2W_SET_UPGRADE_FINISH;
    HI_HAL_SYSLINK_SendMsgToWifiAsync(&stSendMsg);
    
    return AV_SUCCESS;
}

static void OnRecvPirEventFromWifi(HI_SYSLINK_PARM_S stRcvMsg)
{
    if (UpgradeCheckIsRun()) {
        LOGW("upgrade is running!\n");
        return ;
    }
    
    if (HI_SYSLINK_MSGID_W2S_PIR_EVENT == stRcvMsg.u32MsgId)
    {
        LOGW(">>>>>>>> pir event\n");
#ifdef FAC_PIR_OSD_SHOW                
        static int count = 0;
        OsdDrawSetPir(count);
        count++;
#endif /* FAC_PIR_OSD_SHOW */

#ifdef YD_PROTOCOL
        if (!PowerCheckIsLocked()) {
             PowerSetSocSleepTime(1, 20);
        }
        MC_EVENT_ATTR_S stEventAttr;
        memset(&stEventAttr, 0, sizeof(stEventAttr));
        stEventAttr.bPIREvent = 1;
        stEventAttr.PIREvent_utcms = get_utc_ms();
        DOTSetEventState(&stEventAttr);
#else
        if (!PowerCheckIsLocked()) {
             PowerSetSocSleepTime(1, 15);
        }
#endif /* YD_PROTOCOL */
    }
}

 int wifisoc_init(void)
 {
    /* register recv pir event from wifi */
    HI_HAL_SYSLINK_Register(OnRecvPirEventFromWifi);
    
    return 0;
 }

 int wifisoc_deinit(void)
 {
    HI_HAL_SYSLINK_UnRegister(OnRecvPirEventFromWifi);
    
    return 0;
 }

